import request from '@/utils/request'

// 项目文档相关接口
export interface PmProjectDocumentVO {
  docId?: number
  projectId?: number
  taskId?: number
  docName?: string
  docType?: string
  filePath?: string
  fileSize?: number
  fileExtension?: string
  version?: string
  parentId?: number
  sortOrder?: number
  isPublic?: boolean
  downloadCount?: number
  createTime?: string
  updateTime?: string
  remark?: string
  projectName?: string
  taskName?: string
  uploaderName?: string
  fileSizeFormatted?: string
  uploadTimeFormatted?: string
  childrenVo?: PmProjectDocumentVO[]
  hasChildren?: boolean
}

export interface PmProjectDocumentQuery extends PageQuery {
  projectId?: number
  taskId?: number
  parentId?: number
  docType?: string
  docName?: string
  isPublic?: boolean
}

// 查询项目文档列表
export function listDocument(query: PmProjectDocumentQuery) {
  return request({
    url: '/pm/document/list',
    method: 'get',
    params: query
  })
}

// 查询项目文档树结构
export function getDocumentTree(projectId: number, parentId?: number) {
  return request({
    url: '/pm/document/tree',
    method: 'get',
    params: {
      projectId,
      parentId
    }
  })
}

// 查询项目文档详细
export function getDocument(docId: number) {
  return request({
    url: `/pm/document/${docId}`,
    method: 'get'
  })
}

// 新增项目文档
export function addDocument(data: PmProjectDocumentVO) {
  return request({
    url: '/pm/document',
    method: 'post',
    data
  })
}

// 修改项目文档
export function updateDocument(data: PmProjectDocumentVO) {
  return request({
    url: '/pm/document',
    method: 'put',
    data
  })
}

// 删除项目文档
export function delDocument(docIds: number[]) {
  return request({
    url: `/pm/document/${docIds.join(',')}`,
    method: 'delete'
  })
}

// 上传文件
export function uploadFile(file: File, projectId: number, taskId?: number, parentId?: number, docName?: string, isPublic?: boolean) {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('projectId', projectId.toString())
  if (taskId) formData.append('taskId', taskId.toString())
  if (parentId) formData.append('parentId', parentId.toString())
  if (docName) formData.append('docName', docName)
  if (isPublic !== undefined) formData.append('isPublic', isPublic.toString())

  return request({
    url: '/pm/document/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 创建文件夹
export function createFolder(projectId: number, taskId?: number, parentId?: number, folderName?: string, isPublic?: boolean) {
  return request({
    url: '/pm/document/folder',
    method: 'post',
    params: {
      projectId,
      taskId,
      parentId,
      folderName,
      isPublic
    }
  })
}

// 下载文件
export function downloadFile(docId: number) {
  return request({
    url: `/pm/document/download/${docId}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 移动文档
export function moveDocument(docId: number, newParentId: number) {
  return request({
    url: '/pm/document/move',
    method: 'put',
    params: {
      docId,
      newParentId
    }
  })
}

// 复制文档
export function copyDocument(docId: number, newParentId: number) {
  return request({
    url: '/pm/document/copy',
    method: 'post',
    params: {
      docId,
      newParentId
    }
  })
}

// 重命名文档
export function renameDocument(docId: number, newName: string) {
  return request({
    url: '/pm/document/rename',
    method: 'put',
    params: {
      docId,
      newName
    }
  })
}

// 获取文档路径
export function getDocumentPath(docId: number) {
  return request({
    url: `/pm/document/path/${docId}`,
    method: 'get'
  })
}
